package br.com.robson.natanael.bikeasy;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.widget.TextView;

public class LocalizacaoActivity extends Activity {
	private LocationManager locationManager;
	private TextView latitude, longitude, provedor;
	private String urlBase = "http://maps.googleapis.com/maps/api"+
			"/staticmap?size=400x400&sensor=true&markers=color:red|%s,%s";
	
	private WebView mapa;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.localizacao);
		
		latitude = (TextView) findViewById(R.id.latitude);
		longitude = (TextView) findViewById(R.id.longitude);
		provedor = (TextView) findViewById(R.id.provedor);
		
		locationManager = (LocationManager)
				this.getSystemService(Context.LOCATION_SERVICE);
		
		Listener listener = new Listener();
		
		long tempoAtulizacao = 0;
		float distancia = 0;
		
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 
				tempoAtulizacao, distancia, listener);
		
		mapa = (WebView) findViewById(R.id.mapa);
		
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private class Listener implements LocationListener {

    	@Override
    	public void onLocationChanged(Location location) {
    		// TODO Auto-generated method stub
    		String latitudeStr = String.valueOf(location.getLatitude());
    		String longitudeStr = String.valueOf(location.getLongitude());
    		
    		
    		provedor.setText(location.getProvider());
    		latitude.setText(latitudeStr);
    		longitude.setText(longitudeStr);
    		
    		String url = String.format(urlBase, latitudeStr, longitudeStr);
    		mapa.loadUrl(url);
    		
    	}

    	@Override
    	public void onStatusChanged(String provider, int status, Bundle extras) {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	@Override
    	public void onProviderDisabled(String provider) {
    		// TODO Auto-generated method stub
    		
    	}

    	@Override
    	public void onProviderEnabled(String provider) {
    		// TODO Auto-generated method stub
    		
    	}
    }

}



